//
//  SCOrderRefundListController.m
//  shangcheng
//
//  Created by 张艳江 on 2019/6/14.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCOrderRefundListController.h"
#import "SCOrderListCell.h"
#import "SCOrderHeaderView.h"
#import "SCOrderFooterView.h"
#import "SCRefundDetailController.h"

@interface SCOrderRefundListController ()<UITableViewDelegate,UITableViewDataSource,SCOrderFooterViewDelegate>

@property (strong, nonatomic) UITableView    *tableView;
@property (strong, nonatomic) NSMutableArray *dataArray;

@end

@implementation SCOrderRefundListController

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    //请求数据
    [SVProgressHUD showWithStatus:@"加载中..."];
    [self.dataArray removeAllObjects];
    [self requestData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"退款退货申请";
    self.lineView.hidden = NO;
    
    self.view.backgroundColor = BGColor;
    // 设置tableView
    [self setupTableView];
}
- (void)setupTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0.5, kScreenW, kScreenH - kTopHeight) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = BGColor;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 102;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 8)];
    self.tableView.tableFooterView = [UIView new];
    // 注册
    [self.tableView registerNib:[UINib nibWithNibName:@"SCOrderListCell" bundle:nil] forCellReuseIdentifier:@"SCOrderListCellID"];
    [self.view addSubview:self.tableView];
}
#pragma mark - 表格代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    SCModel *model = self.dataArray[section];
    return model.list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SCOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCOrderListCellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SCModel *model = self.dataArray[indexPath.section];
    cell.model = model.list[indexPath.row];
    return cell;
}
#pragma mark - header/footer
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    SCOrderHeaderView *headerView = [SCOrderHeaderView addOrderHeaderView];
    headerView.model = self.dataArray[section];
    headerView.frame = CGRectMake(0, 0, kScreenW, 40);
    return headerView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    SCOrderFooterView *footerView = [SCOrderFooterView addOrderFooterView];
    footerView.tag = section;
    footerView.model = self.dataArray[section];
    footerView.frame = CGRectMake(0, 0, kScreenW, 82);
    footerView.delegate = self;
    return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 82;
}
#pragma mark - 点击了尾部按钮代理事件
- (void)clickButtonWithSection:(NSInteger)section button:(UIButton *)sender{
    
    SCModel *model = self.dataArray[section];
    SCRefundDetailController *vc = [[SCRefundDetailController alloc]init];
    vc.model = model.list[0];
    vc.type = model.type;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 请求数据
- (void)requestData{
    
    NSDictionary *dict = @{@"token":Token,
                           @"status":@"100",
                           @"size":@"10",
                           @"pagenow":@(1)};
    NSString *url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GiftAppOrder&a=App_myorder"];
    //NSLog(@"%@提交的数据%@",Token,dict);
    [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
        NSLog(@"返回的数据:%@",result);
        [SVProgressHUD dismiss];
        NSArray *tempArray = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"data"]];
        for (SCModel *model in tempArray) {
            model.list = [SCModel mj_objectArrayWithKeyValuesArray:model.list];
        }
        [self.dataArray addObjectsFromArray:tempArray];
        [self.tableView reloadData];
    }];
}

@end
